package com.sakura.hk.sakura.springboot.plugin.model;

import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

/**
 * 自定义java文件对象
 * @Author: 贺坤
 * @Date: 2021/7/15 14:09
 */
public class ClassJavaFileObject  extends SimpleJavaFileObject {

    ByteArrayOutputStream outputStream;

    public ClassJavaFileObject(String className, Kind kind) {
        super(URI.create(className + kind.extension), kind);
        // 直接数组输出流
        this.outputStream = new ByteArrayOutputStream();
    }

    /**
     * 打开输出流
     *
     * @return 输出流
     * @throws IOException  IO 异常
     */
    @Override
    public OutputStream openOutputStream() throws IOException {
        return this.outputStream;
    }

    /**
     * 获取输出流内容
     *
     * @return 输出流的内容
     */
    public byte[] getBytes() {
        return this.outputStream.toByteArray();
    }
}